<?php
class App_Model_DbTable_Acl_Role extends Ext_Db_Table
{
    protected $_name = 'acl_role';

    private static $_roleRow = array();
    
    public function getRowById($roleId)
    {        
        if(!$roleId)
        {
            return "";
        }
        
        if(isset(self::$_roleRow[$roleId]))
        {
            return self::$_roleRow[$roleId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $roleId);
        $row = $this->fetchRow($safe);
        
        if (!$row) {
            return "";
        }
        $_roleRow = $row->toArray();
        
        self::$_roleRow[$roleId] = $_roleRow;
        
        return $_roleRow;
    }
    
    public function del($_roleId)
    {
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $_roleId);
        $rows_affected = $this->delete($safe);
    }
    
    public function getAll()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select->from($this->_dbprefix.'acl_role', '*');
        $select->order('id ASC');
        
        //$sql = $select->__toString();
        //echo $sql;

        return $db->fetchAll($select);
    }
    
    public function getAllByRole($roleType)
    {
        if(!in_array($roleType, array('guest', 'member', 'admin'))){
            $roleType = 'guest';
        }
        
        $db = $this->getAdapter();
        $select = $db->select();
        $select->from($this->_dbprefix.'acl_role', '*');
        $select->order('id ASC');
        $select->where('roleType = :roleType');
        $params = array('roleType' => $roleType);
        $select->bind($params);
        
        //$sql = $select->__toString();
        //echo $sql;

        return $db->fetchAll($select);
    }

    public function addNew($inputArray)
    {
        $row = array (
            'roleName'         => $inputArray['roleName'],
            'parentId'         => $inputArray['parentId'],
            'roleType'         => $inputArray['roleType'],
            'description'      => $inputArray['description'],
            'custom'           => $inputArray['custom'],
        );

        try {
            // 插入数据行并返回插入的行数
            $rows_affected = $this->insert($row);
            // 最后插入的数据id   
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $roleId)
    {
        $sql_data_array = array();
        if(array_key_exists('roleName', $inputArray)) $sql_data_array['roleName'] = $inputArray['roleName'];
        if(array_key_exists('parentId', $inputArray)) $sql_data_array['parentId'] = $inputArray['parentId'];
        if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
        
        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $roleId);
            $rows_affected = $this->update($sql_data_array, $where);
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }
}
